// mircoApp/package-deliver-merchant/apply/apply.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    infoData: {},
    realname: "",
    mobile: "",
    imgWaiting: "https://mini-app-img-1251768088.cos.ap-guangzhou.myqcloud.com/images%2FagentShop%2F%E7%AD%89%E5%BE%85%E5%AE%A1%E6%A0%B8%402x.png",

    goodsList: [],
    isLoadMore: true,
    page: 1,
    total_page: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      page: 1,
      isLoadMore: true,
      total_page: 0
    })
    this.getData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.isLoadMore) {
      this.getMoreData();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  toGood(e) {
    console.log(e)
    let {
      id
    } = e.currentTarget.dataset
    wx.navigateTo({
      url: '/packageA/detail_v2/detail_v2?id=' + id,
    })
  },
  confirmBtn(e) {
    wx.navigateTo({
      url: '/packageG/index/index',
    })
  },
  async getData() {
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.package-deliver-merchant.frontend.apply.index")
    })
    if (!data.result) return app.tips(data.msg);
    this.setData({
      infoData: data.data,
      goodsList: data.data.goods.data,
      isLoadMore: true,
      total_page: data.data.goods.last_page
    })
  },
  async getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let {
        data
      } = await app._postNetWork({
        url: app.getNetAddresss("plugin.package-deliver-merchant.frontend.apply.index"),
        data: {
          page: this.data.page
        }
      })
      this.data.isLoadMore = true;
      if (!data.result) {
        app.tips(data.msg)
        // this.$toast(data.msg);
        this.setData({
          page: this.data.page - 1
        })
        this.data.isLoadMore = false;
      } else {
        this.setData({
          goodsList: this.data.goodsList.concat(data.goods.data)
        })
      }
    }
  },
  async applySubmit() {
    if (app._isTextEmpty(this.data.realname)) {
      app.tips("请输入真实姓名")
      return;
    }
    if (app._isTextEmpty(this.data.mobile)) {
      app.tips("请输入手机号")
      return;
    }
    let {
      data
    } = await app._postNetWork({
      url: app.getNetAddresss("plugin.package-deliver-merchant.frontend.apply.apply"),
      data: {
        realname: this.data.realname,
        mobile: this.data.mobile
      }
    })
    app.tips(data.msg)
    if (data.result) {
      this.getData();
    }
  }
})